package com.lm.paymentadmin.DB;

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;


import java.util.Map;


@Configuration
public class ClickHouseDataSourceConfig {

    @Bean
    @ConfigurationProperties("spring.datasource.clickhouse")
    public DataSourceProperties clickhouseDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean(name = "clickhouseDataSource")
    public HikariDataSource clickhouseDataSource(HikariProperties hikariProperties) {
        HikariDataSource dataSource = clickhouseDataSourceProperties().initializeDataSourceBuilder().type(HikariDataSource.class).build();

        for (Map.Entry<String, Object> entry : hikariProperties.getDataSourceProperties().entrySet()) {
            dataSource.addDataSourceProperty(entry.getKey(), entry.getValue());
        }

        return dataSource;
    }

    @Bean(name = "clickhouseTemplate")
    public JdbcTemplate jdbcTemplate(HikariProperties hikariProperties) {
        HikariDataSource ds = clickhouseDataSource(hikariProperties);
        return new JdbcTemplate(ds);
    }
}
